package com.intellagric.service.traceability.Impl.trace_record;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.RecordMachiningDao;
import com.intellagric.mapper.TraceRecordMachiningMapper;
import com.intellagric.module.traceability.RecordMachiningVo;
import com.intellagric.pojo.TraceRecordMachining;
import com.intellagric.pojo.TraceRecordMachiningExample;
import com.intellagric.service.traceability.trace_record.MachiningRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wl
 * @email: 844406720@qq.com
 * @Date: 2018/11/07
 * @DESC： 加工流程记录服务实现
 */
@Service
public class MachiningRecordServiceImpl implements MachiningRecordService {

    @Autowired
    private TraceRecordMachiningMapper recordMachiningMapper;

    @Autowired
    private RecordMachiningDao recordMachiningDao;

    /**
     * 增加加工流程记录
     * @param recordMachining
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addMachiningRecord(TraceRecordMachining recordMachining) {
        if (recordMachiningMapper.insertSelective(recordMachining) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除加工流程记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteIMachiningRecord(String id) {
        if (recordMachiningMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改加工流程记录
     * @param recordMachining
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editMachiningRecord(TraceRecordMachining recordMachining) {
        if (recordMachiningMapper.updateByPrimaryKeySelective(recordMachining) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询加工流程记录详情
     * @param id
     * @return TraceRecordMachining
     */
    @Override
    public TraceRecordMachining getMachiningRecordById(String id) {
        return recordMachiningMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有加工流程记录列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMachiningRecordList() {
        TraceRecordMachiningExample recordMachiningExample = new TraceRecordMachiningExample();
        recordMachiningExample.setOrderByClause("create_date desc");
        recordMachiningExample.createCriteria().andIdIsNotNull();
        List<TraceRecordMachining> recordMachiningList = recordMachiningMapper.selectByExample(recordMachiningExample);
        return LayuiDataGridResult.success().add(recordMachiningList,this.getMachiningRecordList().getCount());
    }

    /**
     * 分页查询所有加工流程记录列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMachiningRecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceRecordMachiningExample recordMachiningExample = new TraceRecordMachiningExample();
        recordMachiningExample.setOrderByClause("create_date desc");
        recordMachiningExample.createCriteria().andIdIsNotNull();
        List<TraceRecordMachining> recordMachiningList = recordMachiningMapper.selectByExample(recordMachiningExample);
        //获取分页信息
        PageInfo<TraceRecordMachining> pageInfo = new PageInfo<>(recordMachiningList);
        return LayuiDataGridResult.success().add(recordMachiningList,pageInfo.getTotal());
    }

    /**
     * 多条件分页查询加工流程记录列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMachiningRecordPageByVo(int page, int rows, RecordMachiningVo recordMachiningVo) {
        PageHelper.startPage(page,rows);
        List<TraceRecordMachining> recordMachiningList = recordMachiningDao.selectMachiningRecordPageByVo(recordMachiningVo);
        //获取分页信息
        PageInfo<TraceRecordMachining> pageInfo = new PageInfo<>(recordMachiningList);
        return LayuiDataGridResult.success().add(recordMachiningList,pageInfo.getTotal());
    }
}
